package com.sino.business.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.sino.bean.Basicinfo;
import com.sino.bean.TblZhuanjialinxuanjieguo;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.bean.TblZhuanjiayuxiangmuguanxi;
import com.sino.business.*;
import com.sino.dao.IExpertManageDAO;
import com.sino.dao.IPatentManageDAO;
import com.sino.model.BasicInfoModel;
import com.sino.model.ExpertAndPatentModel;
import com.sino.model.ExpertModel;
import com.sino.model.ExpertReviewModel;
import com.sino.second.bean.TblZjpsfp;
import com.sino.second.dao.IZjpsfpDAO;
import com.sino.util.DateUtils;
import com.sino.util.SysParameter;

public class ExpertManageServiceImp implements IExpertManageService {

	private IExpertManageDAO expertManageDAO;// 专家管理

	private IPatentManageDAO patentManageDAO;// 专利管理

	private IZjpsfpDAO zjpsfpDAO;// 引入zjpsfpDAO
	
	public void updatePassword(ExpertModel em) {
		TblZhuanjiarukuxinxibiao tzjkx = this.getExpertManageDAO()
				.findExpertsById(em.getFBh());
		tzjkx.setMm(em.getMm());
	}

	public int getExpertReviewPageCount(String hqlwhere) {
		String hql = "select count(*) from  Basicinfo as basicinfo ";
		if (hqlwhere.length() > 0) {
			hql = hql + " where " + hqlwhere;
		}
		return this.getExpertReviewPageCount(hql);
	}

	public List getExpertReviewPageList(int firstRow, int maxRow,
			String hqlwhere) {
		List tempList = new ArrayList();
		String hql = "from  Basicinfo as basicinfo ";
		if (hqlwhere.length() > 0) {
			hql = hql + " where " + hqlwhere;
		}

		hql = hql + " order by basicinfo.tzjlx.size";
		List expReviewList = this.expertManageDAO.findExpertReviewPageList(
				firstRow, maxRow, hqlwhere);
		return this.poListToVoList2(expReviewList);
	}

	public List getExpertList(int firstRow, int maxRow, String hqlwhere) {
		List expertMList = new ArrayList();
		String hql = "from TblZhuanjiarukuxinxibiao as tzjb ";
		if (hqlwhere.length() > 0) {
			hql = hql + " where " + hqlwhere;
		}
		List expertList = this.getExpertManageDAO().findExperts(firstRow,
				maxRow, hql);

		Iterator it = expertList.iterator();
		while (it.hasNext()) {
			TblZhuanjiarukuxinxibiao tzjb = (TblZhuanjiarukuxinxibiao) it
					.next();
			expertMList.add(this.getExpertModel(tzjb));

		}
		return expertMList;
	}

	public int getExpertsCount(String hqlwhere) {
		String hql = "select count(*) from TblZhuanjiarukuxinxibiao as tzjb   ";
		if (hqlwhere.length() > 0) {
			hql = hql + "where " + hqlwhere;
		}

		return this.getExpertManageDAO().findExpertsCount(hql);
	}

	public ExpertModel getExpertsByZlid(String id) {
		TblZhuanjiarukuxinxibiao tzjb = this.getExpertManageDAO()
				.findExpertById(id);
		ExpertModel em = this.getExpertModel(tzjb);
		return em;
	}

	public int getReviewPatentPageCount(String hqlwhere, String assignFlag) {
		String hql = "select count(*) from Basicinfo as basicinfo ";
		if (assignFlag.equals("yesAssign")) {
			hql = hql + " where basicinfo.tzjxm.size>=1";
		} else if (assignFlag.equals("notAssign")) {
			hql = hql + " where basicinfo.tzjxm.size<1";
		}
		if (hqlwhere.length() > 0) {

			hql = hql + " and " + hqlwhere;

		}

		return this.getPatentManageDAO().findNoticePageCount(hql);
	}

	public List getReviewPatentPageList(int firstRow, int maxRow,
			String hqlwhere, String assignFlag) {
		String hql = "from Basicinfo as basicinfo";

		if (assignFlag.equals("yesAssign")) {
			hql = hql + " where basicinfo.tzjxm.size>=1";
		} else if (assignFlag.equals("notAssign")) {
			hql = hql + " where basicinfo.tzjxm.size<1";
		}
		if (hqlwhere.length() > 0) {
			hql = hql + " and " + hqlwhere;
		}

		return this.poListToVoList2(this.patentManageDAO.findBasicinfoPageList(
				firstRow, maxRow, hql));

	}

	public List<BasicInfoModel> poListToVoList2(List<Basicinfo> nList) {
		List<BasicInfoModel> tempList = new ArrayList<BasicInfoModel>();
		Iterator<Basicinfo> it = nList.iterator();
		while (it.hasNext()) {
			BasicInfoModel nm = this.doAssignBasicinfo(it.next());
			tempList.add(nm);

		}

		return tempList;
	}

	public int saveExpertAndBasicinfo(String zlid, String fbh[]) {
		this.delExpertAndBasicinfo(zlid);
		for (int i = 0; i < fbh.length; i++) {
			ExpertAndPatentModel eapm = new ExpertAndPatentModel();
			eapm.setFbh(fbh[i]);
			eapm.setZlid(zlid);
			this.getExpertManageDAO().saveExpertsAndBasicinfo(
					this.getTzjxm(eapm));
		}
		return 1;
	}

	public BasicInfoModel doAssignBasicinfo(Basicinfo basicinfo) {
		BasicInfoModel basicInfoModel = null;

		if (basicinfo.getTzjxm().size() >= 1) {// 判断专家分配状态
			basicInfoModel = this.getBasicModel(basicinfo);
			basicInfoModel.setAssignState("1");
		} else {
			basicInfoModel = this.getBasicModel(basicinfo);
			basicInfoModel.setAssignState("未分配");
		}
		return basicInfoModel;
	}

	public List getExpertsAndBasicinfoByZlid(String id) {

		List expertandbasicinfoList = new ArrayList();
		List tempList = this.getExpertManageDAO()
				.findExpertsAndBasicinfoByZlid(id);
		Iterator it = tempList.iterator();

		while (it.hasNext()) {
			TblZhuanjiayuxiangmuguanxi tzjxm = (TblZhuanjiayuxiangmuguanxi) it
					.next();
			ExpertAndPatentModel eapm = this.getEapm(tzjxm);
			expertandbasicinfoList.add(eapm);

		}

		return expertandbasicinfoList;
	}

	// 专家
	public List<ExpertModel> findExpertByZhuanyema(String zhuanyema) {
		List<TblZhuanjiarukuxinxibiao> expertInfoList = this.expertManageDAO
				.findExpertByZhuanyema(zhuanyema);
		List<ExpertModel> expertModelList = new ArrayList<ExpertModel>();
		for (TblZhuanjiarukuxinxibiao tzjb : expertInfoList) {
			expertModelList.add(this.getExpertModel(tzjb));
		}
		return expertModelList;
	}

	// xyw 项目根据zhuanyema
	public List<BasicInfoModel> findSmByZhuanyema(String hql,String zhuanyema) {
		List<Basicinfo> expertInfoList = this.expertManageDAO
				.findSmByZhuanyema(hql,zhuanyema);

		List<BasicInfoModel> expertModelList = new ArrayList<BasicInfoModel>();
		for (Basicinfo ba : expertInfoList) {
			expertModelList.add(this.getBasicModel(ba));
		}
		return expertModelList;
	}

	// xyw 查找所有
	public List<BasicInfoModel> findAllSms(String hql) {
		List<Basicinfo> list = this.getExpertManageDAO().findAllSms(hql);
		List<BasicInfoModel> ls = new ArrayList<BasicInfoModel>();
		for (Basicinfo ba : list) {
			BasicInfoModel em = this.getBasicModel(ba);
			ls.add(em);
		}
		return ls;
	}

	// xyw 高级查找项目
	public List<BasicInfoModel> findSms(String hqlWhere) {
		String hql = "from Basicinfo basic";
		if (hqlWhere.length() > 0) {
			hql = hql + " where " + hqlWhere;
		}
		hql = hql + " order by basic.sbr desc";
		List<Basicinfo> list = this.expertManageDAO.findSms(hql);
		List<BasicInfoModel> ls = new ArrayList<BasicInfoModel>();
		for (Basicinfo basic : list) {
			BasicInfoModel em = this.getBasicModel(basic);
			ls.add(em);
		}
		return ls;
	}

	public List<ExpertModel> findExperts(String hqlWhere) {
		String hql = "from TblZhuanjiarukuxinxibiao tzjk";
		if (hqlWhere.length() > 0) {
			hql = hql + " where " + hqlWhere;
		}
		hql = hql + " order by tzjk.rkshj desc";
		List<TblZhuanjiarukuxinxibiao> list = this.expertManageDAO
				.findExperts(hql);
		List<ExpertModel> ls = new ArrayList<ExpertModel>();
		for (TblZhuanjiarukuxinxibiao tzj : list) {
			ExpertModel em = this.getExpertModel(tzj);
			ls.add(em);
		}
		return ls;
	}

	public List<ExpertModel> findAllExperts() {
		List<TblZhuanjiarukuxinxibiao> list = this.getExpertManageDAO()
				.findAllExperts();
		List<ExpertModel> ls = new ArrayList<ExpertModel>();
		for (TblZhuanjiarukuxinxibiao tzj : list) {
			ExpertModel em = this.getExpertModel(tzj);
			ls.add(em);
		}
		return ls;
	}

	public int delExpertAndBasicinfo(String zlid) {
		this.expertManageDAO.delExpertsAndBasicinfo(zlid);
		return 1;
	}

	public ExpertModel checkExpertName(String name, String password) {
		boolean flag = false;
		List expertsList = this.expertManageDAO.findExpertByName(name);
		Iterator it = expertsList.iterator();
		while (it.hasNext()) {
			ExpertModel em = this.getExpertModel((TblZhuanjiarukuxinxibiao) it
					.next());
			if (em.getMm() != null && em.getMm().equals(password)) {
				flag = true;
				String hqlWhere="tzpf.year="+SysParameter.CURRENT_YEAR;
				if(true==this.zjpsfpDAO.isXmpsmcExpert(SysParameter.FIRSTREVIEW, em.getFBh(), hqlWhere))
				{
					em.setFirstReview(true);
				}
				if(true==this.zjpsfpDAO.isXmpsmcExpert(SysParameter.SECONDREVIEW, em.getFBh(), hqlWhere))
				{
					em.setSecondReview(true);
				}
				if(true==this.zjpsfpDAO.isXmpsmcExpert(SysParameter.THIRDREVIEW, em.getFBh(), hqlWhere))
				{
					em.setThirdReview(true);
				}
				return em;
			}
		}
		return null;
	}

	// -------------------------------------------- expert po turn vo
	public BasicInfoModel getBasicModel(Basicinfo basicinfo) {
		BasicInfoModel basicInfoModel = new BasicInfoModel();
		basicInfoModel.setHy(basicinfo.getHy().getId());
		basicInfoModel.setLxdh(basicinfo.getLxdh());
		basicInfoModel.setFmr(basicinfo.getFmr());
		basicInfoModel.setLxr(basicinfo.getLxr());
		basicInfoModel.setName(basicinfo.getName());
		basicInfoModel.setPassw(basicinfo.getPassw());
		basicInfoModel.setQxkwmc(basicinfo.getQxkw().getName());
		basicInfoModel.setShbz(basicinfo.getShbz());
		basicInfoModel.setShrq(basicinfo.getShrq());
		basicInfoModel.setSqrq(basicinfo.getSqrq());
		basicInfoModel.setTxdz(basicinfo.getTxdz());
		basicInfoModel.setXk(basicinfo.getXk().getId());
		basicInfoModel.setYddh(basicinfo.getYddh());
		basicInfoModel.setYzbm(basicinfo.getYzbm());
		basicInfoModel.setZlid(basicinfo.getZlid());
		basicInfoModel.setZlid2(basicinfo.getZlid2());
		basicInfoModel.setZlqr(basicinfo.getZlqr());
		basicInfoModel.setZlrq(basicinfo.getZlrq());
		basicInfoModel.setZxbz(basicinfo.getZxbz());
		basicInfoModel.setSbr(basicinfo.getSbr());

		basicInfoModel.setZlid2Link(this.getZlidLink(basicinfo.getZlid2()));
		return basicInfoModel;
	}

	public ExpertModel getExpertModel(TblZhuanjiarukuxinxibiao tzjb) {
		ExpertModel em = new ExpertModel();
		em.setFBh(tzjb.getFBh());
		em.setXm(tzjb.getXm());
		em.setXb(tzjb.getXb());
		em.setXwhchHd(tzjb.getXwhchHd());
		em.setBYShJ(tzjb.getByshJ());
		em.setByyx(tzjb.getByyx());
		em.setChShRq(tzjb.getChShRq());
		em.setDwchZh(tzjb.getDwchZh());
		em.setDzyx(tzjb.getDzyx());
		em.setDwdh(tzjb.getDwdh());
		em.setDzyx(tzjb.getDzyx());
		em.setDwchZh(tzjb.getDwchZh());
		em.setFYhm(tzjb.getFYhm());// users
		em.setGzdw(tzjb.getGzdw());// office
		em.setJj(tzjb.getJj());
		em.setJtdzh(tzjb.getJtdzh());
		em.setJtdhhm(tzjb.getJtdhhm());
		em.setJtdzhyb(tzjb.getJtdzhyb());
		em.setMm(tzjb.getMm());
		em.setMqcshZhY(tzjb.getMqcshZhY());
		em.setShFysh(tzjb.getShFysh());
		em.setShFbd(tzjb.getShFbd());
		em.setShFshCh(tzjb.getShFshCh());
		em.setRkshj(tzjb.getRkshj());
		em.setShXxk(tzjb.getShXxk());
		em.setShHjzh(tzjb.getShHjzh());
		em.setShJ(tzjb.getShJ());
		
		if (null != tzjb.getSysDanweixingzhima()) {
			em.setSysDanweixingzhima(tzjb.getSysDanweixingzhima().getShM());
		}
		if (null != tzjb.getSysShenhezhuangtaima()) {
			em.setSysShenhezhuangtaima(tzjb.getSysShenhezhuangtaima().getShM());
		}
		if (null != tzjb.getSysXueweima()) {
			em.setSysXueweima(tzjb.getSysXueweima().getShM());
		}
		if (null != tzjb.getSysZhichengma()) {
			em.setSysZhichengma(tzjb.getSysZhichengma().getShM());
		}
		if (null != tzjb.getSysZhiwuma()) {
			em.setSysZhiwuma(tzjb.getSysZhiwuma().getShM());
		}
		if (null != tzjb.getSysZhuanyema()) {
			em.setSysZhuanyema(tzjb.getSysZhuanyema().getShM());
		}
		if (null != tzjb.getSysZhuanyema()) {
			em.setSysZhuanyemamcId(tzjb.getSysZhuanyema().getBm());
		}
		if (null != tzjb.getFMz()) {
			em.setFMz(tzjb.getFMz().getShM());
		}
	
//		Set<TblZjpsfp> tzfSet = tzjb.getTblZjpsfpSet();
//		for (TblZjpsfp tzf : tzfSet) {// ==========专家基本信息提取
//			if (SysParameter.FIRSTREVIEW.equals(tzf.getSysXmpsmc().getId())) {
//				em.setFirstReview(true);
//			}
//			if (SysParameter.SECONDREVIEW.equals(tzf.getSysXmpsmc().getId())) {
//				em.setSecondReview(true);
//			}
//			if (SysParameter.THIRDREVIEW.equals(tzf.getSysXmpsmc().getId())) {
//				em.setThirdReview(true);
//			}
//
//		}

		return em;
	}

	public TblZhuanjiayuxiangmuguanxi getTzjxm(ExpertAndPatentModel eapm) {
		TblZhuanjiayuxiangmuguanxi tzjxm = new TblZhuanjiayuxiangmuguanxi();
		TblZhuanjiarukuxinxibiao tzjb = new TblZhuanjiarukuxinxibiao();
		Basicinfo basicinfo = new Basicinfo();
		basicinfo.setZlid(eapm.getZlid());
		tzjb.setFBh(Integer.parseInt(eapm.getFbh()));
		tzjxm.setBasicinfo(basicinfo);
		tzjxm.setTblZhuanjiarukuxinxibiao(tzjb);
		tzjxm.setFprq(DateUtils.getSystemDate());
		return tzjxm;
	}

	public ExpertAndPatentModel getEapm(TblZhuanjiayuxiangmuguanxi tzjxm) {
		ExpertAndPatentModel eapm = new ExpertAndPatentModel();
		eapm.setFbh(String
				.valueOf(tzjxm.getTblZhuanjiarukuxinxibiao().getFBh()));
		eapm.setFprq(tzjxm.getFprq());
		eapm.setZlid(tzjxm.getBasicinfo().getZlid());
		eapm.setXm(tzjxm.getTblZhuanjiarukuxinxibiao().getXm());
		eapm.setPid(tzjxm.getPid());
		return eapm;
	}

	// ----------------------------------------------po and vo
	public ExpertReviewModel getErm(TblZhuanjialinxuanjieguo tzjlx) {
		ExpertReviewModel erm = new ExpertReviewModel();
		erm
				.setFbh(String.valueOf(tzjlx.getTblZhuanjiarukuxinxibiao()
						.getFBh()));
		erm.setZjname(tzjlx.getTblZhuanjiarukuxinxibiao().getXm());
		erm.setPid(tzjlx.getPid());
		erm.setLxzt(tzjlx.getLxzt());
		erm.setZlname((tzjlx.getBasicinfo().getName()));
		erm.setZlid((tzjlx.getBasicinfo().getZlid()));
		erm.setLxrq(tzjlx.getLxrq());
		erm.setZhjyj(tzjlx.getZhjyj());

		return erm;
	}

	public TblZhuanjialinxuanjieguo getTzjlx(ExpertReviewModel erm) {
		TblZhuanjialinxuanjieguo tzjlx = new TblZhuanjialinxuanjieguo();
		Basicinfo basicinfo = new Basicinfo();
		TblZhuanjiarukuxinxibiao tzjb = new TblZhuanjiarukuxinxibiao();
		tzjb.setFBh(Integer.valueOf(erm.getFbh()));
		basicinfo.setZlid(erm.getZlid());
		tzjlx.setTblZhuanjiarukuxinxibiao(tzjb);
		tzjlx.setPid(erm.getPid());
		tzjlx.setLxzt(erm.getLxzt());
		tzjlx.setBasicinfo(basicinfo);
		tzjlx.setLxrq(erm.getLxrq());
		tzjlx.setZhjyj(erm.getZhjyj());

		return tzjlx;

	}

	// 专利链接外网处理
	public String getZlidLink(String zlid) {
		String outZlidStr = zlid;
		if ("zl".equalsIgnoreCase(zlid.substring(0, 2))) {
			outZlidStr = zlid.substring(2);
		}
		return outZlidStr.replaceAll(" ", "");

	}

	// ----------------------------------------------get 或 set 方法

	public IExpertManageDAO getExpertManageDAO() {
		return expertManageDAO;
	}

	public void setExpertManageDAO(IExpertManageDAO expertManageDAO) {
		this.expertManageDAO = expertManageDAO;
	}

	public IPatentManageDAO getPatentManageDAO() {
		return patentManageDAO;
	}

	public void setPatentManageDAO(IPatentManageDAO patentManageDAO) {
		this.patentManageDAO = patentManageDAO;
	}

	/**
	 * @return the zjpsfpDAO
	 */
	public IZjpsfpDAO getZjpsfpDAO() {
		return zjpsfpDAO;
	}

	/**
	 * @param zjpsfpDAO the zjpsfpDAO to set
	 */
	public void setZjpsfpDAO(IZjpsfpDAO zjpsfpDAO) {
		this.zjpsfpDAO = zjpsfpDAO;
	}

}
